﻿using System;
using System.ComponentModel;
using System.Web.UI;

namespace MyLib.Web.UI {
	/// <summary>
	/// 
	/// </summary>
	[DefaultProperty("Digit")]
	[ToolboxData("<{0}:MyPhoneNumberValidator runat=\"server\" ></{0}:MyPhoneNumberValidator>")]
	public sealed class MyPhoneNumberValidator : MyFormatBaseValidator {
		/// <summary>
		/// 
		/// </summary>
		private const int DefaultCountryNumberDigit = 4;

		/// <summary>
		/// 
		/// </summary>
		private const bool DefaultForceDistrictLeadingZero = true;

		/// <summary>
		/// 
		/// </summary>
		private const int DefaultDistrictNumberDigit = 4;

		/// <summary>
		/// 
		/// </summary>
		private const int DefaultDigit = 8;

		/// <summary>
		/// 
		/// </summary>
		private const int DefaultExtDigit = 4;

		/// <summary>
		/// 
		/// </summary>
		[Category("Appearance")]
		[Browsable(true)]
		[Bindable(false)]
		[DefaultValue(DefaultForceDistrictLeadingZero)]
		public bool ForceDistrictLeadingZero {
			get {
				var v = ViewState["ForceDistrictLeadingZero"];
				if (v == null) {
					return DefaultForceDistrictLeadingZero;
				} else {
					return (bool)v;
				}
			}
			set {
				ViewState["ForceDistrictLeadingZero"] = value;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		[Category("Appearance")]
		[Browsable(true)]
		[Bindable(false)]
		[DefaultValue(DefaultCountryNumberDigit)]
		public int CountryNumberDigit {
			get {
				var v = ViewState["CountryNumberDigit"];
				if (v == null) {
					return DefaultCountryNumberDigit;
				} else {
					return (int)v;
				}
			}
			set {
				if (value < 0) {
					throw new ArgumentOutOfRangeException("Value of CountryNumberDigit cannot less than 0, bus was " + value);
				}

				ViewState["CountryNumberDigit"] = value;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		[Category("Appearance")]
		[Browsable(true)]
		[Bindable(false)]
		[DefaultValue(DefaultDistrictNumberDigit)]
		public int DistrictNumberDigit {
			get {
				var v = ViewState["DistrictNumberDigit"];
				if (v == null) {
					return DefaultDistrictNumberDigit;
				} else {
					return (int)v;
				}
			}
			set {
				if (value < 0) {
					throw new ArgumentOutOfRangeException("Value of DistrictNumberDigit cannot less than 0, bus was " + value);
				}

				ViewState["DistrictNumberDigit"] = value;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		[Category("Appearance")]
		[Browsable(true)]
		[Bindable(false)]
		[DefaultValue(DefaultDigit)]
		public int Digit {
			get {
				var v = ViewState["Digit"];
				if (v == null) {
					return DefaultDigit;
				} else {
					return (int)v;
				}
			}
			set {
				if (value < 1) {
					throw new ArgumentOutOfRangeException("Value of Digit cannot less than 1, bus was " + value);
				}

				ViewState["Digit"] = value;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		[Category("Appearance")]
		[Browsable(true)]
		[Bindable(false)]
		[DefaultValue(DefaultExtDigit)]
		public int ExtDigit {
			get {
				var v = ViewState["ExtDigit"];
				if (v == null) {
					return DefaultExtDigit;
				} else {
					return (int)v;
				}
			}
			set {
				if (value < 0) {
					throw new ArgumentOutOfRangeException("Value of ExtDigit cannot less than 0, bus was " + value);
				}

				ViewState["ExtDigit"] = value;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <returns></returns>
		protected override string GetExpression() {
			var exp = new System.Text.StringBuilder();

			if (CountryNumberDigit > 0) {
				exp.AppendFormat(@"(\d{{1,{0}}}\-)?", CountryNumberDigit);
			}
			if (DistrictNumberDigit > 0) {
				if (ForceDistrictLeadingZero && DistrictNumberDigit > 1) {
					exp.AppendFormat(@"(0\d{{1,{0}}}-)?", DistrictNumberDigit - 1);
				} else {
					exp.AppendFormat(@"(\d{{1,{0}}}\-)?", DistrictNumberDigit);
				}
			}

			exp.AppendFormat(@"(\d{{1,{0}}})", Digit);

			if (ExtDigit > 0) {
				exp.AppendFormat(@"(\-\d{{1,{0}}})?", ExtDigit);
			}

			return exp.ToString();
		}
	} // end of MyEmailValidator.
}
